package service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import pojo.Book;
import service.BookManager;

public class BookManagerImpl implements BookManager {

	@Override
	public List<Book> getAllBooks(Connection conn) {

		List<Book> list=new ArrayList<Book>();
		ResultSet rs=null;
		String sql="select * from books";
		try {
			PreparedStatement pst=conn.prepareStatement(sql);
			rs=pst.executeQuery();
			while (rs.next()) {
				Book book=new Book();
				book.setId(rs.getInt("bid"));
				book.setName(rs.getString("bname"));
				list.add(book);				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return list;
	
	}

	@Override
	public Book getBook(int id, Connection conn) {
		String sql="select * from books where bid=?";
		ResultSet rs=null;
		Book b=new Book();
		try {
			PreparedStatement pst=conn.prepareStatement(sql);
			pst.setInt(1, id);
			rs=pst.executeQuery();
			if (rs.next()) {
				b.setId(id);
				b.setName(rs.getString("bname"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return b;
	}

}
